package org.example.groupwarehouse.controller;


import org.apache.ibatis.annotations.Param;
import org.example.groupwarehouse.entity.Admin;
import org.example.groupwarehouse.entity.Category;
import org.example.groupwarehouse.entity.Goods;
import org.example.groupwarehouse.service.CategoryService;
import org.example.groupwarehouse.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;


/**
 * @author Lenovo
 * @version 1.0
 * @data 10:03
 * @className IndexController
 */
@Controller
public class IndexController {
    //注入商品服务  
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private CategoryService categoryService;

    //每页显示的商品数量
    private static int PAGE_SIZE =6;
    //进入首页
    @GetMapping("/")
    public String index(@RequestParam(defaultValue = "1") int page,
                        @RequestParam(required = false) Integer categoryId,
                        @RequestParam(required = false) String goodsname,
                        Model model, HttpSession session) {
        //必须要登录之后才能访问
        Object loginUser = session.getAttribute("loginUser");
        if(loginUser == null){
            return "redirect:/user/login";
        }
        System.out.println("categoryId:"+categoryId);
        if (categoryId != null||goodsname != null) {
            // 获取总记录数
            long total = goodsService.countcategory(categoryId,goodsname);
            // 计算总页数
            int totalPages = (int) Math.ceil((double) total / PAGE_SIZE);
            // 确保页码在有效范围内
            if (page < 1) page = 1;
            //如果页码大于总页数，则设置为总页数
            if (page > totalPages) page = totalPages;

            // 获取当前页的数据
            List<Goods> goodList = goodsService.findByPage2(page, PAGE_SIZE, categoryId,goodsname);

            model.addAttribute("goodList", goodList);
            model.addAttribute("currentPage", page);
            model.addAttribute("totalPages", totalPages);
            model.addAttribute("total", total);

        }else {
            // 获取总记录数
            long total = goodsService.count();
            // 计算总页数
            int totalPages = (int) Math.ceil((double) total / PAGE_SIZE);
            // 确保页码在有效范围内
            if (page < 1) page = 1;
            //如果页码大于总页数，则设置为总页数
            if (page > totalPages) page = totalPages;

            // 获取当前页的数据
            List<Goods> goodList = goodsService.findByPage(page, PAGE_SIZE);

            model.addAttribute("goodList", goodList);
            model.addAttribute("currentPage", page);
            model.addAttribute("totalPages", totalPages);
            model.addAttribute("total", total);
        }
        //获取最热商品
        List<Goods> goodsListHot = goodsService.selectAllHot();
        model.addAttribute("goodsListHot", goodsListHot);
        // 新增：获取所有有效分类
        List<Category> categories = categoryService.getAllCategories();
        model.addAttribute("categories", categories);

        return "index";
    }

}

